package br.com.main.client;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CrudUsuarioView extends Composite implements
		CrudUsuarioPresent.Display {

	private HorizontalPanel painelCadastro;
	private TextBox tbNome;
	private MaskedTextBox tbCpf;
	private TextBox tbLogin;
	private PasswordTextBox pSenha;
	private PasswordTextBox pConfSenha;
	private TextBox tbEmail;
	private MaskedTextBox tbTelefone;
	private MaskedTextBox tbData_nasc;
	private MaskedTextBox tbData_mandato;

	private MaskedTextBox tbCnpj;
	private TextBox tbCep;
	private TextBox tbEstado;
	private TextBox tbCidade;
	private TextBox tbBairro;
	private TextBox tbRua;
	private TextBox tbNumero;
	private TextBox tbComplemento;
	
	private Button btCancelar;
	private Button btCadastrar;
	private DecoratorPanel todo;

	public CrudUsuarioView() {
		init();
		initWidget(painelCadastro);
	}

	private void init() {
		painelCadastro = new HorizontalPanel();
		painelCadastro.addStyleName("borda");
		painelCadastro.addStyleName("bordaCadastro");
		painelCadastro.setSpacing(20);

		tbNome = new TextBox();
		tbNome.setText("Ex: Geyson Daniel");
		tbCpf = new MaskedTextBox(MaskedTextBox.CPF);
		tbLogin = new TextBox();
		pSenha = new PasswordTextBox();
		pConfSenha = new PasswordTextBox();
		tbEmail = new TextBox();
		tbTelefone = new MaskedTextBox(MaskedTextBox.TELEFONE);
		tbData_nasc = new MaskedTextBox(MaskedTextBox.DATA);
		tbData_mandato =new MaskedTextBox(MaskedTextBox.DATA);
		
		VerticalPanel painelSindico = new VerticalPanel();

		painelSindico.add(new Label("Informações do Síndico"));
		painelSindico.add(new Label("Nome"));
		painelSindico.add(tbNome);
		painelSindico.add(new Label("CPF"));
		painelSindico.add(tbCpf);
		painelSindico.add(new Label("Login"));
		painelSindico.add(tbLogin);
		painelSindico.add(new Label("Senha"));
		painelSindico.add(pSenha);
		painelSindico.add(new Label("Confirme sua senha"));
		painelSindico.add(pConfSenha);
		painelSindico.add(new Label("Email"));
		painelSindico.add(tbEmail);
		painelSindico.add(new Label("Telefone"));
		painelSindico.add(tbTelefone);
		painelSindico.add(new Label("Data Nascimento"));
		painelSindico.add(tbData_nasc);
		painelSindico.add(new Label("Data Mandato"));
		painelSindico.add(tbData_mandato);
		
		tbCnpj = new MaskedTextBox(MaskedTextBox.CNPJ);
		tbCep = new TextBox();
		tbEstado = new TextBox();
		tbEstado.setMaxLength(2);
		tbEstado.setWidth("25px");
		tbCidade = new TextBox();
		tbBairro = new TextBox();
		tbRua = new TextBox();
		tbNumero = new TextBox();
		tbComplemento = new TextBox();
		
		cepAdd(false);
		
		VerticalPanel painelCondominio = new VerticalPanel();
		
		painelCondominio.add(new Label("Informações do Condomínio"));
		painelCondominio.add(new Label("CNPJ"));
		painelCondominio.add(tbCnpj);
		painelCondominio.add(new Label("Cep"));
		painelCondominio.add(tbCep);
		painelCondominio.add(new Label("Estado"));
		painelCondominio.add(tbEstado);
		painelCondominio.add(new Label("Cidade"));
		painelCondominio.add(tbCidade);
		painelCondominio.add(new Label("Bairro"));
		painelCondominio.add(tbBairro);
		painelCondominio.add(new Label("Rua"));
		painelCondominio.add(tbRua);
		painelCondominio.add(new Label("Número"));
		painelCondominio.add(tbNumero);
		painelCondominio.add(new Label("Complemento"));
		painelCondominio.add(tbComplemento);
		painelCondominio.add(new Label(" "));
		
		btCancelar = new Button("Cancelar");
		btCancelar.addStyleName("botaoCancelar");
		btCadastrar = new Button("Cadastrar");
		btCadastrar.addStyleName("botaoCadastrar");

		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.add(btCancelar);
		hPanel.add(btCadastrar);
		
		painelCondominio.add(hPanel);
		
		painelCadastro.add(painelSindico);
		painelCadastro.add(painelCondominio);

		todo = new DecoratorPanel();
		todo.add(painelCadastro);
	}
	
	private void cepAdd(boolean b) {
		tbEstado.setEnabled(b);
		tbCidade.setEnabled(b);
		tbBairro.setEnabled(b);
	}

	@Override
	public String[] getCampos() {
		String[] dados = new String[14];
		dados[0] = tbCpf.getText();
		dados[1] = tbNome.getText();
		dados[2] = tbLogin.getText();
		dados[3] = pSenha.getText();
		dados[4] = tbEmail.getText();
		dados[5] = tbTelefone.getText();
		dados[6] = tbData_nasc.getText();
		dados[7] = tbData_mandato.getText();
		dados[8] = tbCnpj.getText();
		dados[9] = tbCep.getText();
		dados[10] = tbEstado.getText();
		dados[11] = tbCidade.getText();
		dados[12] = tbBairro.getText();
		dados[13] = tbRua.getText();
		dados[14] = tbNumero.getText();
		dados[15] = tbComplemento.getText();
		return dados;
	}

	@Override
	public Button getBtCadastrar() {
		return btCadastrar;
	}

	@Override
	public TextBox getTbCep() {
		return tbCep;
	}

	@Override
	public void doGet(String cep) {
		String url = "http://cep.republicavirtual.com.br/web_cep.php?cep="
				+ cep + "&formato=json";

		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
				URL.encode(url));

		try {
			@SuppressWarnings("unused")
			Request request = builder.sendRequest(null, new RequestCallback() {
				public void onError(Request request, Throwable exception) {
					// Couldn't connect to server (could be timeout, SOP
					// violation, etc.)
				}

				public void onResponseReceived(Request request,
						Response response) {
					if (200 == response.getStatusCode()) {
						JSONValue jsonVal = JSONParser.parseStrict(response
								.getText());
						JSONObject jsonObj = jsonVal.isObject();

						String uf = jsonObj.get("uf").toString();
						String cidade = jsonObj.get("cidade").toString();
						String bairro = jsonObj.get("bairro").toString();

						tbEstado.setText(uf.substring(1, uf.length() - 1));
						tbCidade.setText(cidade.substring(1,
								cidade.length() - 1));
						tbBairro.setText(bairro.substring(1,
								bairro.length() - 1));
						cepAdd(true);
					} else {

					}
				}
			});
		} catch (RequestException e) {
			// Couldn't connect to server
		}
	}

	@Override
	public TextBox getTbNome() {
		return this.tbNome;
	}

	@Override
	public Button getBtCancelar() {
		return btCancelar;
	}

	@Override
	public void cleanCampos() {
		tbEstado.setValue("");
		tbBairro.setValue("");
		tbCidade.setValue("");
		tbNome.setValue("");
		tbCpf.setValue("");
		tbLogin.setValue("");
		pSenha.setValue("");
		pConfSenha.setValue("");
		tbCep.setValue("");
		tbRua.setValue("");
		tbNumero.setValue("");
		tbTelefone.setValue("");
		tbData_nasc.setValue("");
	}
}
